package gameSite;
import java.util.*;
import java.sql.*;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JDBCdriver {

	
	   // JDBC driver name and database URL
	   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	   static  String DB_URL = "jdbc:mysql://clipper.encs.concordia.ca:3306/soen387b";
	  // static final String DB_URL = "jdbc:mysql://localhost:3306/Comp6411";

	 //  Database credentials
	   static final String USER = "soen387b";
	   static final String PASS = "f32k64";
	   static int lport, rport;
	   static String rhost;
	   
	   
	   public static void go()
	   {
	        String user = "s_sukhd";
	        String password = "HNn+mXVh";
	        String host = "login.encs.concordia.ca";
	        int port=22;
	        try
	            {
	            JSch jsch = new JSch();
	            Session session = jsch.getSession(user, host, port);
	            lport = 3306;
	            rhost = "clipper.encs.concordia.ca";
	            rport = 3306;
	            session.setPassword(password);
	            session.setConfig("StrictHostKeyChecking", "no");
	            System.out.println("Establishing Connection...");
	            session.connect();
	            int assinged_port=session.setPortForwardingL(lport, rhost, rport);
	            System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
	            }
	        catch(Exception e){System.err.print(e);}
	        
	    }
	   
	   
	   
	    Connection getConnectionToDatabase()
	   {
		  // boolean verified = false;
		   
		   go();
	 	   Connection conn = null;

		      //STEP 2: Register JDBC driver
		    try 
		    {
				Class.forName("com.mysql.jdbc.Driver");
			} 
		    catch (ClassNotFoundException e) 
		    {
				e.printStackTrace();
			}
		      DB_URL = "jdbc:mysql://localhost:"+lport+"/soen387b";
		      //STEP 3: Open a connection
		      System.out.println("Connecting to database...");
		      
		    try 
		    {
				  conn = DriverManager.getConnection(DB_URL,USER,PASS);
			      System.out.println("Connected to database...");
			} 
		    catch (SQLException e) 
		    {
				e.printStackTrace();
			}
		   
		   //Connection loginConnection = conn;
		   
		   return conn;
	   }
	   
}
